
#include "basedock.h"
#include <QPushButton>
#include <QBoxLayout>
#include "splitter.h"
#include "containers.h"


class QPlaceHolderWidget : public QWidget {
public:
    QPlaceHolderWidget(QWidget* parent = 0):QWidget(parent){

    }
};

QBaseDock::QBaseDock(QWidget *parent):QDockItemContainer(parent)
{
    m_left = 0;
    m_right = 0;
    m_top = 0;
    m_bottom = 0;
    m_tabbed = 0;
    m_layout = 0;
    m_centerwidget = 0;
    this->setLayout(new QHBoxLayout());//just so there is some layout
    this->layout()->setMargin(0);
    this->setCenterWidget(new QPushButton(this));
}


void QBaseDock::setCenterWidget(QWidget *widget)
{
//    if (widget == 0){
//        widget = new QPlaceHolderWidget();
//    }
    if (m_centerwidget){
        //TODO
    }
    if (widget == 0)
        return;
    m_centerwidget = widget;
    if (m_layout){

    }
    else {//m_layout == 0
        this->layout()->addWidget(m_centerwidget);
    }
    m_centerwidget->show();

}

void QBaseDock::dock(QDockItem *dragged_widget, DockOption dockoption)
{
    if (m_layout == 0){
        USplitter* splitter;
        switch (dockoption){
        case DockOption::Top:
        case DockOption::Left:
            if (dockoption == DockOption::Top){
                splitter = new USplitter(Qt::Vertical);
            }else{//left
                splitter = new USplitter(Qt::Horizontal);
            }
            splitter->insertWidget(0,dragged_widget);
            splitter->insertWidget(1,this->layout()->itemAt(0)->widget());
            setLayoutWidget(splitter);
            break;
        case DockOption::Right:
        case DockOption::Bottom:
            if (dockoption == DockOption::Bottom){
                splitter = new USplitter(Qt::Vertical);
            }else{//right
                splitter = new USplitter(Qt::Horizontal);
            }
            splitter->insertWidget(0,this->layout()->itemAt(0)->widget());
            splitter->insertWidget(1,dragged_widget);

            setLayoutWidget(splitter);
            break;
        }
    }
    else //m_layout != 0
    {
        if (dockoption & DockOption::CROSS){
            USplitter* splitter = qobject_cast<USplitter*>(m_layout);
            if (splitter){
                bool sameorientation;
                if (splitter->orientation() == Qt::Horizontal){
                    sameorientation = dockoption & DockOption::Horizontal;
                } else {//vertical
                    sameorientation = dockoption & DockOption::Vertical;
                }
                if (sameorientation){
                    if ( dockoption & (DockOption::Left | DockOption::Top)){
                        splitter->insertWidget(0,dragged_widget);
                    } else {
                        splitter->insertWidget(splitter->count(),dragged_widget);
                    }
                } else {//other orientation
                    Qt::Orientation orient;
                    if (splitter->orientation() == Qt::Horizontal){
                        orient = Qt::Vertical;
                    }
                    else {
                        orient = Qt::Horizontal;
                    }
                    USplitter* splitter = new USplitter(orient);

                    if ( dockoption & (DockOption::Left | DockOption::Top)){
                        splitter->insertWidget(0,dragged_widget);
                        splitter->insertWidget(1,m_layout);
                    } else {
                        splitter->insertWidget(0,m_layout);
                        splitter->insertWidget(splitter->count(),dragged_widget);
                    }
                    setLayoutWidget(splitter);
                }

            }
        }
    }

}


bool QBaseDock::noContent()
{
    if (m_layout == 0 && m_centerwidget == 0){
        return true;
    }
    return false;
}


void QBaseDock::removeWidget(QWidget *widget)
{
    //TODO
}


void QBaseDock::replaceWidget(QWidget *old_widget, QWidget *new_widget)
{
    Q_ASSERT(m_layout == old_widget);

    setLayoutWidget(new_widget);
}

void QBaseDock::setLayoutWidget(QWidget *widget)
{
    if (m_layout)
        this->layout()->removeWidget(m_layout);
    m_layout = widget;
    this->layout()->addWidget(widget);
}


//void USplitter::childEvent(QChildEvent *event)
//{
//    if (event->added()){
//        if (qobject_cast<QPreviewWidget*>(event->child())){
//            return;
//        }
//        QSplitter::childEvent(event);
//    }
//    else {
//        QSplitter::childEvent(event);
//    }
//}

//void QBaseDock::resizeEvent(QResizeEvent *ev)
//{
//    if (m_centerwidget && !m_layout){
//        m_centerwidget->setGeometry(QRect(QPoint(0,0),ev->size()));
//    }
//}

